package com.imooc.controller;

import com.imooc.common.enums.ProductStatusEnum;
import com.imooc.common.enums.ResultEnum;
import com.imooc.common.form.ProductForm;
import com.imooc.common.utils.KeyUtil;
import com.imooc.dataobject.ProductCategory;
import com.imooc.dataobject.ProductInfo;
import com.imooc.service.CategoryService;
import com.imooc.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/seller/product")
public class SellerProductController {

    @Autowired
    ProductService productService;

    @Autowired
    CategoryService categoryService;

    /**
     * 查询商品列表
     *
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        Page<ProductInfo> productInfoPage = productService.findAll(pageRequest);
        Map<String, Object> param = new HashMap();
        param.put("productInfoPage", productInfoPage);
        param.put("currentPage", page);
        param.put("currentSize", size);
        return new ModelAndView("product/list", param);
    }

    /**
     * 商品上架
     *
     * @param productId
     * @return
     */
    @RequestMapping("/onSale")
    public ModelAndView onSale(@RequestParam("productId") String productId) {
        Map<String, Object> params = new HashMap();
        try {
            productService.onSale(productId);
        } catch (Exception e) {
            params.put("msg", e.getMessage());
            params.put("url", "/sell/seller/product/list");
            return new ModelAndView("common/error", params);
        }

        params.put("msg", ResultEnum.SUCCESS.getMessage());
        params.put("url", "/sell/seller/product/list");
        return new ModelAndView("common/success", params);
    }

    /**
     * 商品下架
     *
     * @param productId
     * @return
     */
    @RequestMapping("/offSale")
    public ModelAndView offSale(@RequestParam("productId") String productId) {
        Map<String, Object> params = new HashMap();
        try {
            productService.offSale(productId);
        } catch (Exception e) {
            params.put("msg", e.getMessage());
            params.put("url", "/sell/seller/product/list");
            return new ModelAndView("common/error", params);
        }

        params.put("msg", ResultEnum.SUCCESS.getMessage());
        params.put("url", "/sell/seller/product/list");
        return new ModelAndView("common/success", params);
    }

    /**
     * 新增/修改页
     *
     * @param productId
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId", required = false) String productId) {
        Map<String, Object> params = new HashMap<>();
        if (!StringUtils.isBlank(productId)) {
            ProductInfo productInfo = productService.findOne(productId);
            params.put("productInfo", productInfo);
        }

        List<ProductCategory> productCategoryList = categoryService.findAll();
        params.put("productCategoryList", productCategoryList);
        return new ModelAndView("product/index", params);
    }

    /**
     * 新增/修改 提交保存
     *
     * @param productForm
     * @return
     */
    @RequestMapping("/save")
    public ModelAndView save(ProductForm productForm) {
        Map<String, Object> params = new HashMap();
        try {
            ProductInfo productInfo = new ProductInfo();
            if (!StringUtils.isBlank(productForm.getProductId())) {
                productInfo = productService.findOne(productForm.getProductId());
                BeanUtils.copyProperties(productForm, productInfo);
            } else {
                BeanUtils.copyProperties(productForm, productInfo);
                productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
                productInfo.setProductId(KeyUtil.genUniqueKey());
            }

            productService.save(productInfo);
        } catch (Exception e) {
            params.put("msg", e.getMessage());
            params.put("url", "/sell/seller/product/index");
            return new ModelAndView("common/error", params);
        }

        params.put("msg", ResultEnum.SUCCESS.getMessage());
        params.put("url", "/sell/seller/product/list");
        return new ModelAndView("common/success", params);
    }
}
